package com.example.springbootgraduation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springbootgraduation.entity.BanJi;
import com.example.springbootgraduation.entity.XueYuan;
import com.example.springbootgraduation.service.BanJiService;
import com.example.springbootgraduation.service.XueYuanService;
import com.example.springbootgraduation.vo.BanJiVo;
import com.example.springbootgraduation.vo.DataView;
import com.example.springbootgraduation.vo.XueYuanVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/*班级*/
@Controller
@RequestMapping("/banji")
public class BanJiController {
    @Autowired
    private BanJiService banJiService;

    /**
     * 跳转班级管理
     */
    @RequestMapping("/toBanJi")
    public String toNews(){
        return "banji/banji";
    }

    /**
     *查询所有班级
     */
    @RequestMapping("/listBanJi")
    @ResponseBody
    public DataView listBanJi(BanJiVo banJiVo){
        QueryWrapper<BanJi> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNoneBlank(banJiVo.getName()),"name",banJiVo.getName());
        /*创建分页对象*/
        IPage<BanJi> iPage = new Page<>(banJiVo.getPage(),banJiVo.getLimit());
        banJiService.page(iPage,queryWrapper);
        return new DataView(iPage.getTotal(),iPage.getRecords());//getTotal获得所有，getRecords数据
    }

    /**
     * 删除
     */
    @RequestMapping("/deleteById")
    @ResponseBody
    public DataView deleteById(Integer id){
        banJiService.removeById(id);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("班级删除成功！");
        return dataView;
    }

    /**
     * 新增或者修改
     */
    @RequestMapping("/addOrUpdateBanJi")
    @ResponseBody
    public DataView addOrUpdateBanJi(BanJi banji){
        banJiService.saveOrUpdate(banji);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("班级新增或者修改成功！");
        return dataView;
    }
}
